package com.ruoyi.znjc.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.znjc.domain.ZnjcTimers;
import com.ruoyi.znjc.service.IZnjcTimersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警定时Controller
 * 
 * @author ruoyi
 * @date 2025-07-26
 */
@RestController
@RequestMapping("/znjc/timers")
public class ZnjcTimersController extends BaseController
{
    @Autowired
    private IZnjcTimersService znjcTimersService;

    /**
     * 查询预警定时列表
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZnjcTimers znjcTimers)
    {
        startPage();
        List<ZnjcTimers> list = znjcTimersService.selectZnjcTimersList(znjcTimers);
        return getDataTable(list);
    }

    /**
     * 导出预警定时列表
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:export')")
    @Log(title = "预警定时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnjcTimers znjcTimers)
    {
        List<ZnjcTimers> list = znjcTimersService.selectZnjcTimersList(znjcTimers);
        ExcelUtil<ZnjcTimers> util = new ExcelUtil<ZnjcTimers>(ZnjcTimers.class);
        util.exportExcel(response, list, "预警定时数据");
    }

    /**
     * 获取预警定时详细信息
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:query')")
    @GetMapping(value = "/{timerId}")
    public AjaxResult getInfo(@PathVariable("timerId") Long timerId)
    {
        return success(znjcTimersService.selectZnjcTimersByTimerId(timerId));
    }

    /**
     * 新增预警定时
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:add')")
    @Log(title = "预警定时", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnjcTimers znjcTimers)
    {
        return toAjax(znjcTimersService.insertZnjcTimers(znjcTimers));
    }

    /**
     * 修改预警定时
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:edit')")
    @Log(title = "预警定时", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnjcTimers znjcTimers)
    {
        return toAjax(znjcTimersService.updateZnjcTimers(znjcTimers));
    }

    /**
     * 删除预警定时
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:remove')")
    @Log(title = "预警定时", businessType = BusinessType.DELETE)
	@DeleteMapping("/{timerIds}")
    public AjaxResult remove(@PathVariable Long[] timerIds)
    {
        return toAjax(znjcTimersService.deleteZnjcTimersByTimerIds(timerIds));
    }


    /**
     * 根据定时器ID查询关联的规则内容
     *
     * 通过定时器ID，查询对应的规则内容，如果未找到则返回错误信息
     *
     * @param timerId 定时器主键
     * @return 规则内容或错误提示
     */
    @PreAuthorize("@ss.hasPermi('znjc:timers:query')")
    @GetMapping(value = "/ruleContent/{timerId}")
    public AjaxResult getRuleContentByTimerId(@PathVariable("timerId") Long timerId) {
        Map<String, Object> ruleData = znjcTimersService.selectRuleInfoByTimerId(timerId);
        if (ruleData != null && ruleData.get("rule_content") != null) {
            // 返回规则内容字段，或直接返回整个 Map
            return success(ruleData.get("rule_content"));
        } else {
            return error("未找到对应的规则内容");
        }
    }


}
